﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Pikapika
{
    public static class ScriptCssExtensions
    {
        private static Dictionary<StandardLibraries, string> _standardLibraries = new Dictionary<StandardLibraries, string>
        {
            {StandardLibraries.Jquery, "http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"},
            {StandardLibraries.JqueryUI, "jquery-ui-1.8.18.custom.min.js"},
            {StandardLibraries.MicrosoftAjax, "http://ajax.microsoft.com/ajax/4.0/1/MicrosoftAjax.js"}, 
            {StandardLibraries.MicrosoftMvcAjax, "MicrosoftMvcAjax.js"},
            {StandardLibraries.MicrosoftMvcValidation, "MicrosoftMvcValidation.js"},
        };


        public static MvcHtmlString Script(this HtmlHelper helper, StandardLibraries lib)
        {
            return Script(helper, _standardLibraries[lib]);
        }

        public static MvcHtmlString Script(this HtmlHelper helper, string file)
        {
            if (helper == null)
                throw new ArgumentNullException("helper", "helper is null.");
            if (String.IsNullOrEmpty(file))
                throw new ArgumentException("file is null or empty.", "file");

            var regKey = "_ScriptReg_" + file.ToLowerInvariant();
            if (helper.ViewData.ContainsKey(regKey))
            {
                return MvcHtmlString.Empty;
            }
            helper.ViewData[regKey] = true;

            string src;
            if (IsRelativeToDefaultPath(file))
            {
                src = "~/Scripts/" + file;
            }
            else
            {
                src = file;
            }
            TagBuilder scriptTag = new TagBuilder("script");
            scriptTag.MergeAttribute("type", "text/javascript");
            scriptTag.MergeAttribute("src", UrlHelper.GenerateContentUrl(src, helper.ViewContext.HttpContext));
            return MvcHtmlString.Create(scriptTag.ToString(TagRenderMode.Normal));

        }

        public static MvcHtmlString Css(this HtmlHelper helper, string file, string media = "")
        {
            if (helper == null)
                throw new ArgumentNullException("helper", "helper is null.");
            if (String.IsNullOrEmpty(file))
                throw new ArgumentException("file is null or empty.", "file");

            var regKey = "_CssReg_" + file.ToLowerInvariant();
            if (helper.ViewData.ContainsKey(regKey))
            {
                return MvcHtmlString.Empty;
            }
            helper.ViewData[regKey] = true;

            string src;
            if (IsRelativeToDefaultPath(file))
            {
                src = "~/Content/" + file;
            }
            else
            {
                src = file;
            } 
            TagBuilder linkTag = new TagBuilder("link");
            linkTag.MergeAttribute("type", "text/css");
            linkTag.MergeAttribute("rel", "stylesheet");
            linkTag.MergeAttribute("href", UrlHelper.GenerateContentUrl(src, helper.ViewContext.HttpContext));
            if (!string.IsNullOrWhiteSpace(media))
            {
                linkTag.MergeAttribute("media", media);
            }
            return MvcHtmlString.Create(linkTag.ToString(TagRenderMode.SelfClosing));
        }

        private static bool IsRelativeToDefaultPath(string file)
        {
            return (((!file.StartsWith("~", StringComparison.Ordinal) && !file.StartsWith("../", StringComparison.Ordinal)) && (!file.StartsWith("/", StringComparison.Ordinal) && !file.StartsWith("http://", StringComparison.OrdinalIgnoreCase))) && !file.StartsWith("https://", StringComparison.OrdinalIgnoreCase));
        }


    }
}